﻿using System;
using System.Collections.Generic;
using System.Linq;
using studentSubsidy.Entity.Message;
using studentSubsidy.Models;
using studentSubsidy.Utils;
using Message = studentSubsidy.Models.Message;

namespace studentSubsidy.Dao
{
    public class MessageDao
    {
        private MyDbContext DBContext = new MyDbContext();

        public void addMessage(MessageQuery messageQuery,int user_id)
        {
            Message message = new Message
            {
                id = GenerateID.GenForInt(),
                user_id = user_id,
                author = messageQuery.author,
                message = messageQuery.message,
                isRead = 1,
                cdt = DateTime.Now.ToString("u")
            };
            DBContext.Message.Add(message);
            DBContext.SaveChanges();
        }
        
        public void updateMessage(int userId)
        {
            List<Message> messages = DBContext.Message.Where(p => p.user_id == userId).ToList();

            foreach (var VARIABLE in messages)
            {
                VARIABLE.isRead = 1;
                DBContext.Message.Update(VARIABLE);
            }
            DBContext.SaveChanges();
        }
        
        public List<Message> getMessageById(int user_id)
        {
            return DBContext.Message.Where(p => p.user_id == user_id).ToList();
        }
        
        public User getUserById(int user_id)
        {
            return DBContext.User.Where(p => p.id == user_id).ToList().First();
        }
    }
}